package com.example.bruger.androidneurosky;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by fuke9_000 on 27-03-2015.
 */
public class DbHighScoreHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 4;
    private static final String DATABASE_NAME = "NeuroskyGame";
    private static final String DATABASE_CREATE_TABLE = "create table " + "HighScore" + " (ID integer primary key autoincrement, " + " Score float not null, " +
            "Name text" + ")";

    private static final String TABLE_HIGHSCORE = "HighScore";

    //Employee table column names
    private static final String KEY_ID = "ID";
    private static final String KEY_SCORE = "Score";
    private static final String KEY_NAME = "Name";
    private static final String[] COLUMNS = {KEY_ID, KEY_SCORE, KEY_NAME};

    DbHighScoreHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS HighScore");
        onCreate(db);
    }

    public void addHighScore(HighScore score) {
        Log.d("addHighScore", score.toString());

        SQLiteDatabase db =  this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_SCORE, score.getHighScore());
        values.put(KEY_NAME, score.getName());

        db.insert(TABLE_HIGHSCORE, null, values);
        db.close();
        Log.d("HighScore inserted!", score.toString());
    }

    public HighScore getHighScore(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_HIGHSCORE, COLUMNS, "ID = ?", new String[] { String.valueOf(id)}, null, null, null);

        if(cursor != null) {
            cursor.moveToFirst();
        }

        HighScore score = new HighScore();
        score.setId(Integer.parseInt(cursor.getString(0)));
        score.setHighScore(Float.parseFloat(cursor.getString(1)));
        score.setName(cursor.getString(2));
        Log.d("Score fetched!", score.toString());
        return score;
    }

    public List<HighScore> getAllScores() {
        List<HighScore> scores = new LinkedList<HighScore>();

        String query = "SELECT * FROM " + TABLE_HIGHSCORE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        HighScore score = null;
        if(cursor.moveToFirst()) {
            do {
                score = new HighScore();
                score.setId(Integer.parseInt(cursor.getString(0)));
                score.setHighScore(Float.parseFloat(cursor.getString(1)));
                score.setName(cursor.getString(2));

                scores.add(score);
            }
            while (cursor.moveToNext());
        }
        return scores;
    }

    public List<HighScore> getTopTen() {
        List<HighScore> scores = new LinkedList<HighScore>();

        String query = "SELECT * FROM " + TABLE_HIGHSCORE + " ORDER BY Score DESC LIMIT 10";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        HighScore score = null;
        if(cursor.moveToFirst()) {
            do {
                score = new HighScore();
                score.setId(Integer.parseInt(cursor.getString(0)));
                score.setHighScore(Float.parseFloat(cursor.getString(1)));
                score.setName(cursor.getString(2));

                scores.add(score);
            }
            while (cursor.moveToNext());
        }
        return scores;
    }

    public void deleteScore(HighScore score) {
        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_HIGHSCORE, KEY_ID + " = ?", new String[] { String.valueOf(score.getId())});
        db.close();

        Log.d("HighScore deleted!", score.toString());
    }

    public void deleteAllScores() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_HIGHSCORE);
        Log.d("All highscores deleted!", "");
    }
}
